{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import os\n",
    "from tqdm import tqdm\n",
    "import json\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "ann_path = r'/media/alvinai/Documents/underwater/ahf_work_dirs/testb/testBresult_epoch_12_no_waterseeds.json' # annotation json\n",
    "train_path = 'seacoco/testB.json'\n",
    "img_path = 'seacoco/testB/'\n",
    "save_path = 'seacoco/pred_testb_with_bbox/' # the path of saveing image with annotated bboxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(ann_path,'r') as f:\n",
    "    ann = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'image_id': 1,\n",
       " 'bbox': [726.2694091796875,\n",
       "  451.71466064453125,\n",
       "  187.04254150390625,\n",
       "  101.61077880859375],\n",
       " 'score': 0.4945487380027771,\n",
       " 'category_id': 1}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ann[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(train_path, 'r') as f1:\n",
    "    train = json.load(f1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "ann = {'images':train['images'], 'annotations':ann, 'categories':train['categories']}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 1200/1200 [03:03<00:00,  6.53it/s]\n"
     ]
    }
   ],
   "source": [
    "for ann_img in tqdm(ann['images']):\n",
    "    img = cv2.imread(img_path + ann_img['file_name'])\n",
    "    img_id = ann_img['id']\n",
    "    for ann_ann in ann['annotations']:\n",
    "        if ann_ann['image_id'] == img_id and ann_ann['score']>=0.1:\n",
    "            x1 = int(ann_ann['bbox'][0])\n",
    "            y1 = int(ann_ann['bbox'][1])\n",
    "            x2 = int(ann_ann['bbox'][0] + ann_ann['bbox'][2])\n",
    "            y2 = int(ann_ann['bbox'][1] + ann_ann['bbox'][3])\n",
    "            img = cv2.rectangle(img, (x1,y1), (x2,y2), (255,0,0), 8)\n",
    "            for cat in ann['categories']:\n",
    "                if cat['id'] == ann_ann['category_id']:\n",
    "                    catname = cat['name']\n",
    "                    break\n",
    "            txt = catname + str(round(ann_ann['score'],1))\n",
    "            img = cv2.putText(img, txt, (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 4)\n",
    "    cv2.imwrite(save_path + ann_img['file_name'], img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
